{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random,numpy\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "mi,mx = 1,10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = random.randint(mi,mx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2372"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 5000\n",
      "2 2500\n",
      "3 1250\n",
      "4 1875\n",
      "5 2187\n",
      "6 2343\n",
      "7 2421\n",
      "8 2382\n",
      "9 2362\n",
      "10 2372\n",
      "------------ 2372\n"
     ]
    }
   ],
   "source": [
    "x,y = mi,mx\n",
    "i = 0\n",
    "while(True):\n",
    "    v = int((x+y)/2)\n",
    "    i+=1\n",
    "    print(i,v)\n",
    "    if v == r:\n",
    "        print(\"------------\",r)\n",
    "        break\n",
    "    if v < r :\n",
    "        x,y = v,y\n",
    "    else:\n",
    "        x,y = x,v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = []\n",
    "for i in range(0,1000):\n",
    "    r = random.randint(mi,mx)\n",
    "    x,y = mi,mx\n",
    "    i = 0\n",
    "    while(True):\n",
    "        v = int((x+y)/2)\n",
    "        i+=1\n",
    "        if v == r:\n",
    "            #print(\"------------\",r)\n",
    "            break\n",
    "        if v < r :\n",
    "            x,y = v,y\n",
    "        else:\n",
    "            x,y = x,v\n",
    "    n.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12.41"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.mean(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  2.,   2.,   8.,  15.,  20.,  45.,  97., 232., 375., 204.]),\n",
       " array([ 5. ,  5.9,  6.8,  7.7,  8.6,  9.5, 10.4, 11.3, 12.2, 13.1, 14. ]),\n",
       " <a list of 10 Patch objects>)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAQKElEQVR4nO3df6xfdX3H8edrFPFnLIxrV9tml7hOg2YWcoc4l8XB1ILLislGSjatjqQuwU0Xs1lcMnUZS81UMrONpQ6kLgxsUEMj6OyQxJgM8IKlUJBxJ2DbFXoVQRgZW/G9P+5hfNve9t7b7/3eb/n0+Ui++Z7zPr/e96R93XM/95zvTVUhSWrLzwy7AUnS/DPcJalBhrskNchwl6QGGe6S1KBFw24A4NRTT63R0dFhtyFJLyh33HHHD6tqZLplx0S4j46OMj4+Puw2JOkFJcnDh1vmsIwkNchwl6QGGe6S1CDDXZIaZLhLUoMMd0lqkOEuSQ0y3CWpQYa7JDXomHhCVZIARjfcOJTjPrTx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FJ8sdJdia5J8m1SV487J4WUpKrkuxLck9P7ZQk25I80L2fPMweF9Jhzsdfd/9XdiT5SpLFw+xxIU13PnqWfThJJTl1Po5luM+PvwG+XlWvA94I3DfkfoYiyTLgj4CxqnoDU79MXzvcrhbc1cDqg2obgJuraiVwczd/vLiaQ8/HNuANVfVLwL8Dly50U0N0NYeeD5KsAN4O/GC+DmS49ynJK4FfA64EqKr/qarHh9vVUC0CXpJkEfBS4D+H3M+CqqpvAY8dVF4DbO6mNwMXLGhTQzTd+aiqb1TV/m72VqaegzkuHObfB8DlwJ8C83aHi+Hev9OASeDzSb6b5B+TvGzYTQ1DVe0BPsXU1cde4Imq+sZwuzomLKmqvd30I8CSYTZzjPl94GvDbmKYkqwB9lTVXfO5X8O9f4uAM4ErquoM4L84vn7s/n/dWPIapr7hvRp4WZLfG25Xx5aauvfY+4+BJH8G7AeuGXYvw5LkpcBHgT+f730b7v3bDeyuqtu6+euZCvvj0W8AD1bVZFX9L/Bl4FeG3NOx4NEkSwG6931D7mfokrwX+E3gd+v4ftjmNUxdDN2V5CGmhqjuTPJz/e7YcO9TVT0C7Ery2q50LgP4aOMXiB8AZyd5aZIwdS6Oy18uH2QrsK6bXgfcMMRehi7JaqbGl3+rqp4edj/DVFV3V9Wrqmq0qkaZulg8s8uVvhju8+MPgWuS7ABWAX815H6Govvp5XrgTuBupv59HVePmie5Fvg34LVJdie5GNgIvC3JA0z9dLNxmD0upMOcj78FXgFsS7I9yT8MtckFdJjzMZhjHd8/EUlSm7xyl6QGGe6S1CDDXZIaZLhLUoMMd0lqkOEuSQ0y3CWpQf8H1sQrXExMuCoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
